package com.example.yoho.mvp.view.fragment;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerBrandComponent;
import com.example.yoho.di.moudles.BrandModules;
import com.example.yoho.mvp.contract.BrandContract;
import com.example.yoho.mvp.model.entity.BrandGoodsEntity;
import com.example.yoho.mvp.model.entity.BrandListEntity;
import com.example.yoho.mvp.model.entity.BrandPageEntity;
import com.example.yoho.mvp.model.entity.BrandTabEntity;
import com.example.yoho.mvp.model.entity.MenuEntity;
import com.example.yoho.mvp.presenter.BrandPresenter;
import com.example.yoho.mvp.view.activity.IndexView;
import com.example.yoho.mvp.view.adapter.MyBrandListAdapter;
import com.example.yoho.mvp.view.adapter.MyTabBrandAdapter;
import com.example.yoho.utils.MyCustom;
import com.example.yoho.utils.MyGlide;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryFragment extends BaseFragment<BrandPresenter> implements BrandContract.View {
    String url="http://169.254.88.63/yoho/";
    private MyBrandListAdapter adapter;
    private android.support.v7.widget.RecyclerView brandListRecv;
    private List<CustomTabEntity> list_tab=new ArrayList<>();
    private List<CustomTabEntity> list_category=new ArrayList<>();
    private com.flyco.tablayout.CommonTabLayout brandlistComtab;
    private List<String> list_banners=new ArrayList<>();
    private String flag = "";
    private List<BrandPageEntity> list_page = new ArrayList<>();
    private List<BrandListEntity.Values> list_br = new ArrayList<>();
    private List<BrandListEntity.Values> list_brandTab=new ArrayList<>();
    private MyTabBrandAdapter myTabBrandAdapter;
    private com.example.yoho.mvp.view.activity.IndexView letters;
    private LinearLayoutManager linearLayoutManager;

    @Override
    public int bondLayout() {
        return R.layout.fragment_category;
    }

    @Override
    public void Inject() {
        DaggerBrandComponent.builder().brandModules(new BrandModules(this)).build().Inject(this);
    }

    @Override
    public void initView() {

        brandListRecv = (RecyclerView) findviewByIds(R.id.brandList_recv);
        brandlistComtab = (CommonTabLayout) findviewByIds(R.id.brandlist_comtab);
        adapter=new MyBrandListAdapter(list_page);
        brandListRecv.setAdapter(adapter);
         linearLayoutManager = new LinearLayoutManager(getContext());
        brandListRecv.setLayoutManager(linearLayoutManager);
        letters = (IndexView) findviewByIds(R.id.letters);
    }

    @Override
    public void initData() {
        mPresenter.BrandRequestAll(1,1);


    }

    @Override
    public void showMenu(MenuEntity menuEntity) {
        for (int i = 0; i < menuEntity.getValues().size(); i++) {
            list_tab.add(new MyCustom(menuEntity.values.get(i).getMenu_name(),0,0));
        }
        brandlistComtab.setTabData((ArrayList<CustomTabEntity>) list_tab);

    }

    @Override
    public void showBrandTab(List<BrandTabEntity.ValuesBean> brandTabEntities) {



    }

    private void MyImageLoaders(Banner banner) {
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                MyGlide.GlideAS(context,path,imageView);
            }
        });
    }

    @Override
    public void showBrandGoods(List<BrandGoodsEntity.ValuesBean> brandGoodsEntities) {

    }

    @Override
    public void showBrandList(BrandListEntity brandListEntity) {
        Log.i("ysk", "showBrandList: "+brandListEntity.getValues().toString());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.brandlist_headview, null);

        Banner banner=view.findViewById(R.id.brand_banners);

        CommonTabLayout commonTabLayout=view.findViewById(R.id.categorys_comtab);
        list_category.add(new MyCustom("全部品牌",0,0));
        list_category.add(new MyCustom("注入新品牌",0,0));
        list_category.add(new MyCustom("热门品牌",0,0));
        commonTabLayout.setTabData((ArrayList<CustomTabEntity>) list_category);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position){
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        RecyclerView recyclerView=view.findViewById(R.id.brandlist_recyc);


        list_brandTab.addAll(brandListEntity.getValues());

        myTabBrandAdapter=new MyTabBrandAdapter(R.layout.category_item,list_brandTab);

        recyclerView.setAdapter(myTabBrandAdapter);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), brandListEntity.getValues().size());
        recyclerView.setLayoutManager(gridLayoutManager);



        for (int a = 0; a < brandListEntity.getValues().size(); a++) {
            String category_img_path = brandListEntity.getValues().get(a).getBrand_icon();
            list_banners.add(url+category_img_path);
        }

        banner.setImages(list_banners);

        MyImageLoaders(banner);

        banner.start();

        adapter.addHeaderView(view);

        List<BrandListEntity.Values> list = brandListEntity.getValues();
        list_br.addAll(brandListEntity.getValues());
        for (int i = 0; i < list.size(); i++) {
            String brand_letter = list.get(i).getBrand_letter();
            if (brand_letter.equals(flag)) {
                list_page.add(new BrandPageEntity(list.get(i).getBrand_id(),
                        list.get(i).getMenu_id(),
                        list.get(i).getBrand_letter(),
                        list.get(i).getBrand_name(),
                        list.get(i).getHot_tag(),
                        list.get(i).getBrand_time(),
                        list.get(i).getBrand_icon(),
                        list.get(i).getBrand_bg(),
                        2));
            } else {
                flag = brand_letter;
                list_page.add(new BrandPageEntity("", "",
                        list.get(i).getBrand_letter(), "",
                        "", "", "", "",
                        1));
                list_page.add(new BrandPageEntity(list.get(i).getBrand_id(),
                        list.get(i).getMenu_id(),
                        list.get(i).getBrand_letter(),
                        list.get(i).getBrand_name(),
                        list.get(i).getHot_tag(),
                        list.get(i).getBrand_time(),
                        list.get(i).getBrand_icon(),
                        list.get(i).getBrand_bg(),
                        2));
            }


        }
            Log.i("ysk", "showBrandList: "+list_page.toString());
            adapter.notifyDataSetChanged();
            letters.setOnIndexChangeListener(new IndexView.OnIndexChangeListener() {
            @Override
            public void onIndexChange(String word) {
                for (int i = 0; i < list_page.size(); i++) {
                    if (word.equals(list_page.get(i).getBrand_letter())){

                        linearLayoutManager.scrollToPositionWithOffset(i,0);
                    }
                }
            }
        });
    }
}
